package com.saas.servlet;

import com.saas.entity.Student;
import com.saas.service.IStudentService;
import com.saas.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/stus")
public class AllStudentServlet extends HttpServlet {

    private IStudentService iss = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");

        List<Student> list = iss.getAllStudents();
        System.out.println(list);

        PrintWriter out = resp.getWriter();

        out.print("<table border='1' align='center' width='80%'>");
        out.print("<tr>");
        out.print("<td>编号</td>");
        out.print("<td>姓名</td>");
        out.print("<td>性别</td>");
        out.print("<td>班级编号</td>");
        out.print("<td>管理</td>");
        out.print("</tr>");

        for (Student s : list) {
            out.print("<tr>");
            out.print("<td>" + s.getStu_id() + "</td>");
            out.print("<td>" + s.getStu_name() + "</td>");
            out.print("<td>" + s.getStu_sex() + "</td>");
            out.print("<td>" + s.getC_id() + "</td>");
            out.print("<td><a href='/save?id=" + s.getStu_id() + "'>保存</a></td>");
            out.print("<td><a href='/update?id=" + s.getStu_id() + "'>修改</a></td>");
            out.print("<td><a href='/delete?id=" + s.getStu_id() + "'>删除</a></td>");
            out.print("</tr>");
        }
        out.print("</table>");
    }
}
